const Koa = require('koa')


// 实例化应用
const app = new Koa()

async function cookieParser(ctx, next) {
    const headerCookie = ctx.headers.cookie
    ctx.state.cookies = {}
    if(headerCookie) {
        const cookies = headerCookie.split(';')
        cookies.forEach(cookie => {
            const parts = cookie.split('=')
            ctx.state.cookies[parts[0]] = parts[1]
        });
    }
    await next()

}


app.use(cookieParser)

app.use(async (ctx) => {
    console.log('router');
    ctx.body = ctx.state.cookies
})

app.listen(8899, () => {
    console.log('listen on 8899');
})